<template>
  <header>
    <div class="logo">
      <span @click="goBack"><</span>
    </div>
    <div class="search">
      <i class="iconfont icon-sousuo"></i>
      <!-- <span>搜索您喜欢的产品...</span> -->
      <form action="" onsubmit="return false" @keyup.enter="goSearchList">
        <input
          type="search"
          placeholder="搜索您喜欢的产品..."
          v-model="searchInput"
        />
      </form>
    </div>
    <div class="kefu">
      <span @click="goSearchList">搜索</span>
    </div>
  </header>
</template>

<script>
export default {
  data() {
    return {
      searchInput: this.$route.query.key || "",
      historyList: [],
    };
  },
  methods: {
    goSearchList() {
      // 如果输入的是空值 直接退出
      if (!this.searchInput.trim()) {
        return;
      }
      // 如果本地不存在历史记录
      if (!localStorage.getItem("searchList")) {
        localStorage.setItem("searchList", []);
      } else {
        // 如果本地存在历史记录  直接取出
        this.historyList = JSON.parse(localStorage.getItem("searchList")) || [];
      }
      this.historyList.unshift(this.searchInput.trim());
      // 去重
      let set = new Set(this.historyList);
      localStorage.setItem("searchList", JSON.stringify(Array.from(set)));

      if (this.$route.query.key == this.searchInput) {
        return false;
      }
      // 跳转
      this.$router.push({
        name: "SearchList",
        query: {
          key: this.searchInput,
        },
      });
    },
    goBack() {
      this.$router.back();
    },
    
  },
};
</script>

<style scoped>
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #b0352f;
  width: 100vw;
  height: 1.2rem;
}
.logo {
  width: 50px;
  height: 100%;
}
.logo span {
  display: block;
  text-align: center;
  line-height: 1.2rem;
  font-size: 30px;
  color: #eee;
}
.search {
  font-size: 14px;
  display: flex;
  align-items: center;
  width: 240px;
  height: 30px;
  background-color: #fff;
  border-radius: 15px;
  color: #ccc;
  flex: 1;
}

.search i {
  padding: 0 10px;
}
.search input {
  border: 0;
  outline: none;
  width: 120%;
  font-size: 14px;
}
.kefu {
  height: 100%;
  width: 50px;
  margin-right: 5px;
}
.kefu span {
  font-family: "Courier New", Courier, monospace;
  color: #ccc;
  display: block;
  font-size: 18px;
  height: 100%;
  width: 100%;
  text-align: center;
  line-height: 50px;
}
</style>